package com.siwei.mes.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.ExperimentTask;
import com.siwei.mes.entity.tgl.TglItemorder;
import com.siwei.mes.entity.tgl.TglTrwd;
import com.siwei.mes.mapper.ExperimentItemorderMapper;
import com.siwei.mes.mapper.ExperimentTaskMapper;
import com.siwei.mes.service.ExperimentTaskService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 试验台账与任务关系(ExperimentTask)表服务实现类
 *
 * @author linzi
 * @since 2023-12-18 22:11:48
 */
@Service
public class ExperimentTaskServiceImpl implements ExperimentTaskService {
    @Resource
    private ExperimentTaskMapper experimentTaskMapper;
    @Resource
    private ExperimentItemorderMapper experimentItemorderMapper;

    @Override
    public Long create(ExperimentTask entity) {
        experimentTaskMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(ExperimentTask entity) {
        ExperimentTask experimentTask = experimentTaskMapper.selectByPrimaryKey(entity.getId());
        assertTrue(experimentTask == null, "试验台账与任务关系信息不存在");
        experimentTaskMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        ExperimentTask experimentTask = experimentTaskMapper.selectByPrimaryKey(id);
        assertTrue(experimentTask == null, "试验台账与任务关系信息不存在");
        experimentTaskMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ExperimentTask> getPage(PageQuery<ExperimentTask, ExperimentTask> query) {
        ExperimentTask searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> experimentTaskMapper.getList(searchParam));
    }

    @Override
    public ExperimentTask loadById(Long id) {
        return experimentTaskMapper.loadById(id);
    }

    @Override
    public List<ExperimentTask> getAll(ExperimentTask entity) {
        return experimentTaskMapper.getList(entity);
    }

    @Override
    public List<TglTrwd> selectByExperimentId(Long experimentId) {
        List<TglTrwd> tglTrwdList = experimentTaskMapper.selectByExperimentId(experimentId);
        /**key :frwdh value 对应的小票发货方量*/
        Map<Integer, Float> tglItemorderMap = new HashMap<>();
        /**获取对应试验台账下所有的小票信息*/
        List<TglItemorder> tglItemorderList = experimentItemorderMapper.selectByExperimentId(experimentId);
        for (TglItemorder itemorder : tglItemorderList) {
            float fhQuantity = 0;
            if (tglItemorderMap.containsKey(itemorder.getFrwdh())) {
                fhQuantity = tglItemorderMap.get(itemorder.getFrwdh());
            }
            fhQuantity += itemorder.getFhquantity();
            tglItemorderMap.put(itemorder.getFrwdh(), fhQuantity);
        }
        /**直接遍历复制*/
        for (TglTrwd tglTrwd : tglTrwdList) {
            if (tglItemorderMap.containsKey(tglTrwd.getFrwdh())) {
                tglTrwd.setFhquantity(tglItemorderMap.get(tglTrwd.getFrwdh()));
            }
        }
        return tglTrwdList;
    }
}
